package com.example.book.service;

import com.example.book.enums.BookStatus;
import com.example.book.mapper.BookInfoMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public Integer insertBookInfo(BookInfo bookInfo) {
       return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        //获取总记录数
        Integer count = bookInfoMapper.count();

        //获取当前页的数据
        List<BookInfo> bookInfoList = bookInfoMapper.selectBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());

        //处理状态
        //状态 0:删除 1:可借阅 2:不可借阅
        for (BookInfo bookInfo:bookInfoList) {
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }

        return new PageResult(bookInfoList, count, pageRequest);
    }
}
